package com.gzist.blog_system.controller;

import com.gzist.blog_system.domain.Article;
import com.gzist.blog_system.domain.Statistic;
import com.gzist.blog_system.domain.result.Result;
import com.gzist.blog_system.service.StatisticService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/statistic")
@Api(tags = "文章统计信息模块")
public class StatisticController {

    @Resource
    private StatisticService statisticService;

    /*
     * @description:
     *  新增文章对应的统计信息
     * @author: 黄泽建
     **/
    @PostMapping
    @ApiOperation("新增文章对应的统计信息")
    public Result add(@RequestBody @ApiParam("新增文章") Article article) {
        boolean result = statisticService.add(article);
        log.info("操作到的数据====>{}", result);
        if (!result) return Result.error("新增失败！");
        return Result.success();
    }

    /*
     * @description:
     *  根据文章id查询点击量和评论量信息
     * @author: 黄泽建
     **/
    @PostMapping("/getStatistic")
    @ApiOperation("根据文章id查询点击量和评论量信息")
    public Result add(@RequestParam @ApiParam("文章id") Long articleId) {
        Statistic statistic = statisticService.selectOneByArticleId(articleId);
        log.info("操作到的数据====>{}", statistic);
        return Result.success(statistic);
    }

    /*
     * @description:
     *  根据文章id更新点击量
     * @author: 黄泽建
     **/
    @PostMapping("/updateHits")
    @ApiOperation("根据文章id更新点击量")
    public Result updateHits(@RequestBody @ApiParam("文章统计数据") Statistic statistic) {
        boolean result = statisticService.updateHits(statistic);
        log.info("操作到的数据====>{}", result);
        if (!result) return Result.error("更新失败！");
        return Result.success();
    }
    /*
     * @description:
     *  根据文章id更新评论量
     * @author: 黄泽建
     **/
    @PostMapping("/updateComments")
    @ApiOperation("根据文章id更新评论量")
    public Result updateComments(@RequestBody @ApiParam("文章统计数据") Statistic statistic) {
        boolean result = statisticService.updateComments(statistic);
        log.info("操作到的数据====>{}", result);
        if (!result) return Result.error("更新失败！");
        return Result.success();
    }

    /*
     * @description:
     *  根据文章id删除统计数据
     * @author: 黄泽建
     **/
    @DeleteMapping("/delete")
    @ApiOperation("根据文章id删除统计数据")
    public Result delete(@RequestParam @ApiParam("文章id") Long id) {
        boolean result = statisticService.deleteStatistic(id);
        log.info("操作到的数据====>{}", result);
        if (!result) return Result.error("删除失败！");
        return Result.success();
    }

    /*
     * @description:
     *  统计文章热度信息
     * @author: 黄泽建
     **/
    @GetMapping("/getStatistic")
    @ApiOperation("统计文章热度信息")
    public Result getStatistic() {
        List<Statistic> list = statisticService.getStatistic();
        log.info("操作到的数据====>{}", list);
        return Result.success(list);
    }

    /*
     * @description:
     *  统计博客文章总访问量
     * @author: 黄泽建
     **/
    @GetMapping("/getTotalVisit")
    @ApiOperation("统计博客文章总访问量")
    public Result getTotalVisit() {
        Long result = statisticService.getTotalVisit();
        log.info("操作到的数据====>{}", result);
        return Result.success(result);
    }

    /*
     * @description:
     *  统计博客文章总评论量
     * @author: 黄泽建
     **/
    @GetMapping("/getTotalComment")
    @ApiOperation("统计博客文章总评论量")
    public Result getTotalComment() {
        Long result = statisticService.getTotalComment();
        log.info("操作到的数据====>{}", result);
        return Result.success(result);
    }


}
